package cn.sytton.taffe.web.flw.controller;

import cn.sytton.taffe.framework.base.entity.PageParam;
import cn.sytton.taffe.web.flw.service.IFlowDefService;
import cn.sytton.taffe.web.flw.entity.FlowDefEntity;
import cn.sytton.taffe.web.flw.service.impl.FlowDefService;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程定义 控制层
 *
 * @author skyrock
 */
@Tag(name = "工作流定义")
@RequiredArgsConstructor
@RestController
@RequestMapping("/flw/flow-def")
public class FlowDefController {

    private final IFlowDefService flowDefService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "流程部署")
    @PostMapping("/deploy")
    public void deploy(@RequestBody FlowDefEntity entity) {
        flowDefService.deploy(entity);
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "查询流程定义xml结构")
    @Parameter(name = "deploymentId", description = "流程部署ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/xml/{deploymentId}")
    public String findXml(@PathVariable String deploymentId) {
        return flowDefService.findXml(deploymentId);
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "根据key查询流程定义xml结构")
    @Parameter(name = "key", description = "key", in = ParameterIn.PATH, required = true)
    @GetMapping("/xml/key/{key}")
    public String findXmlByKey(@PathVariable String key) {
        return flowDefService.findXmlByKey(key);
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "查询所有流程定义列表")
    @GetMapping("/list")
    public Map<String, List<FlowDefEntity>> findDefList() {
        return flowDefService.findDefList();
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "查询流程版本列表")
    @GetMapping("/version/{key}")
    public List<FlowDefEntity> findVersionList(@PathVariable String key) {
        return flowDefService.findVersionListByKey(key);
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "查询所有流程定义分页列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @GetMapping("/page")
    public PageInfo<FlowDefEntity> findDefPage(PageParam pp) {
        return flowDefService.findDefPage(pp);
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "检查流程key是否存在")
    @Parameter(name = "key", description = "流程key", in = ParameterIn.PATH, required = true)
    @GetMapping("/check-key/{key}")
    public Boolean checkKey(@PathVariable String key) {
        return flowDefService.checkKey(key);
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "检查流程name是否存在")
    @Parameter(name = "name", description = "流程名称", in = ParameterIn.PATH, required = true)
    @GetMapping("/check-name/{name}")
    public Boolean checkName(@PathVariable String name) {
        return flowDefService.checkName(name);
    }

    @ApiOperationSupport(order = 7)
    @Operation(summary = "根据流程Key查询流程的UserTask")
    @Parameter(name = "key", description = "流程Key", in = ParameterIn.PATH, required = true)
    @GetMapping("/user-task/def-key/{key}")
    public List<FlowDefService.FlowUserTask> findUserTaskByDefKey(@PathVariable String key) {
        return flowDefService.findUserTaskByDefKey(key);
    }
    @ApiOperationSupport(order = 8)
    @Operation(summary = "根据流程定义Id查询流程的UserTask")
    @Parameter(name = "key", description = "流程Id", in = ParameterIn.PATH, required = true)
    @GetMapping("/user-task/def-id/{id}")
    public List<FlowDefService.FlowUserTask> findUserTaskByDefId(@PathVariable String id) {
        return flowDefService.findUserTaskByDefId(id);
    }
}
